﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Statistic
{
    sealed public class SkyWalkerReader : IReader
    {
        int i = 1;
    
        /**
         * Get all advertisement links
         * @return advs
         *   A list which contains all advertisement links
         */
        IList<string> IReader.GetLinks()
        {
            IList<string> advs = new List<string>();
            string htmlCode;
            bool continueSearch = true;
            while (continueSearch)
            {               
                for (int k = 2; k < 12; k++)
                {
                    htmlCode = Protocol.GetHtmlCode("http://www.skywalker.gr/advertsMenu.aspx?sectorID=15&fromRow=" + i);
                    string SearchString = "ctl00_masterContent_advertsGridView_ctl0"+k+"_titleLinkButton";
                    if (k >= 10)
                    {
                        SearchString = "ctl00_masterContent_advertsGridView_ctl" + k + "_titleLinkButton";
                    }

                    int firstChr = htmlCode.IndexOf(SearchString, StringComparison.OrdinalIgnoreCase);
                    if (firstChr == -1)
                    {
                        break;
                    }
                    int lastChr = htmlCode.Length - 1;
                    int yo = lastChr - firstChr;
                    string cuttedHtmlCode = htmlCode.Substring(firstChr, yo);
                    string cleanedHtmlCode = HtmlString.CleanSource(cuttedHtmlCode, "href=" + "\"", "\"" + ">", "sky");
                    string newUrl = "http://www.skywalker.gr/" + cleanedHtmlCode;
                    advs.Add(newUrl);
                }
                i = i + 10;
                htmlCode = Protocol.GetHtmlCode("http://www.skywalker.gr/advertsMenu.aspx?sectorID=15&fromRow=" + i);
                continueSearch = HtmlString.PageExists(htmlCode, "advertPageLinkCurrentClass'>1<");
            }
            return advs;
        }

        IList<Advertisements> IReader.DoTheWork()
        {
            Protocol statsProtocol = new Protocol();
            IList<string> advs = new List<string>();
            IList<Advertisements> advsResult = new List<Advertisements>();
            IReader reader = new SkyWalkerReader();
            advs = reader.GetLinks();

            for (int i = 0; i < advs.Count; i++)
            {
                Advertisements advertisements = new Advertisements();
                string htmlCode = Protocol.GetHtmlCode(advs[i].ToString());
                string cleanedhtmlCode = HtmlString.CleanSource(htmlCode, "<ul>", "<tr><td valign=" + "\"" + "bottom" + "\"" + ">&nbsp;</td></tr>", "sky");
                cleanedhtmlCode = HtmlString.StripTagsCharArray(cleanedhtmlCode);
                advertisements.Content = cleanedhtmlCode;
                DateTime date = HtmlString.GetDate(htmlCode, "Ημερομηνία δημοσίευσης/ανανέωσης:", "sky");
                advertisements.Date = date;
                advsResult.Add(advertisements);
            }
            return advsResult;
        }
    }
}
